package com.zj.osm.utils.db_utils;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zj.osm.model.pojo.EquipModuleType;
import com.zj.osm.model.pojo.Module;
import com.zj.osm.model.pojo.ModuleType;
import com.zj.osm.utils.SQLiteDatabaseUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : luhenchang
 *     e-mail : 1276998208@qq.com
 *     time   : 2019/12/03
 *     desc   : 版权所有
 *     version: 1.0
 * </pre>
 */
public class ModuleUtils {
    private static final String SELECT_FROM_MODULE="select * from Module m where m.ModuleTypeKey =";
    private static final ModuleUtils ourInstance = new ModuleUtils();
    public static ModuleUtils getInstance() {
        return ourInstance;
    }

    private ModuleUtils() {
    }
    /**
     * 获取杆高这些信息
     *
     * @param
     */
    public ArrayList<Module> getGangaoResult(String moduleTypeKey) {
        ArrayList<Module> module_array = new ArrayList<>();
         SQLiteDatabase sqLiteDatabase = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        @SuppressLint("Recycle") Cursor cursor = sqLiteDatabase.rawQuery( SELECT_FROM_MODULE+ "'" + moduleTypeKey + "'", null);//此处的db是表名，name是列名
        while (cursor.moveToNext()) {
            Module module = new Module();
            module.setModuleID(cursor.getString(0));
            module.setModuleName(cursor.getString(1));
            module.setModuleTypeKey(cursor.getString(2));
            module.setModuleCode(cursor.getString(3));
            module_array.add(module);
        }
        cursor.close();
        return module_array;
    }
    private List<ModuleType> allMT = new ArrayList<>();
    /**
     * @param lever 这个用来获取设备id的
     */
    public List<Module> getModuleList(int lever) {
        List<EquipModuleType> strings = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        @SuppressLint("Recycle") Cursor cursor = sqLiteDatabase.rawQuery("select * from equip_moduleType where equipID=" + "'" + lever + "'", null);//此处的db是表名，name是列名
        while (cursor.moveToNext()) {
            EquipModuleType equip = new EquipModuleType();
            equip.setId(cursor.getInt(0));
            equip.setEquipID(cursor.getInt(1));
            equip.setModuleTypeID(cursor.getInt(2));
            strings.add(equip);
        }
        cursor.close();
        Log.e("haha2=", strings.toString());
        List<ModuleType> mtlst = new ArrayList<>();
        for (EquipModuleType et : strings) {
            @SuppressLint("Recycle") Cursor cursorm = sqLiteDatabase.rawQuery("select * from ModuleType where moduletypeID=" + "'" + et.getModuleTypeID() + "'", null);//此处的db是表名，name是列名
            while (cursorm.moveToNext()) {
                ModuleType mt = new ModuleType();
                mt.setModuleTypeID(cursorm.getString(0));
                mt.setName(cursorm.getString(1));
                mt.setModuleTypeKey(cursorm.getString(2));
                mt.setParentKey(cursorm.getString(3));
                mtlst.add(mt);
            }
            cursorm.close();
        }

        List<ModuleType> mtall = new ArrayList<>(mtlst);
        for (ModuleType mt : mtlst) {
            allMT.clear();
            mtall.addAll(getSubModuleTypeList(mt.getModuleTypeKey()));
        }

        List<Module> mAll = new ArrayList<>();
        for (ModuleType mt : mtall) {
            @SuppressLint("Recycle") Cursor cursorm = sqLiteDatabase.rawQuery("select * from Module where moduletypekey=" + "'" + mt.getModuleTypeKey() + "'", null);//此处的db是表名，name是列名
            while (cursorm.moveToNext()) {
                Module m = new Module();
                m.setModuleID(cursorm.getString(0));
                m.setModuleName(cursorm.getString(1));
                m.setModuleTypeKey(cursorm.getString(2));
                m.setModuleCode(cursorm.getString(3));
                m.setRemark(cursorm.getString(6));
                mAll.add(m);
            }
            cursorm.close();
        }
        return mAll;
    }

    private List<ModuleType> getSubModuleTypeList(String parentKey) {
        List<ModuleType> mtlst = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        @SuppressLint("Recycle") Cursor cursorm = sqLiteDatabase.rawQuery("select * from ModuleType where ParentKey=" + "'" + parentKey + "'", null);//此处的db是表名，name是列名
        while (cursorm.moveToNext()) {
            ModuleType mt = new ModuleType();
            mt.setModuleTypeID(cursorm.getString(0));
            mt.setName(cursorm.getString(1));
            mt.setModuleTypeKey(cursorm.getString(2));
            mt.setParentKey(cursorm.getString(3));
            mtlst.add(mt);
        }
        allMT.addAll(mtlst);
        cursorm.close();
        for (ModuleType mt : mtlst) {
            getSubModuleTypeList(mt.getModuleTypeKey());
        }
        return allMT;
    }
}
